#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include "ui.h"
#include "../client/client.h"
#include "../chess_logic/chess_logic.h"

// 移除重复的空白函数声明
// 只保留需要实际实现的函数

void draw_chess_board(ChessGame *game) {
    erase();
    
    // 显示游戏模式
    if (game->player_is_white) {
        printw("Mode: Player(White) vs AI(Black)");
    } else {
        printw("Mode: Player(Black) vs AI(White)");
    }
    
    // 顶部坐标标签 - 每个字母之间增加空格以适应更宽的格子
    printw("\n   ");
    for (char c = 'a'; c <= 'h'; c++) {
        printw("%c  ", c);  // 两个空格而不是一个
    }
    printw("\n");
    
    for (int row = 0; row < 8; row++) {
        // 左侧行号
        printw("%d ", 8 - row);
        
        for (int col = 0; col < 8; col++) {
            // 设置棋盘颜色
            if ((row + col) % 2 == 0) {
                attron(COLOR_PAIR(1)); // 浅色格子
            } else {
                attron(COLOR_PAIR(2)); // 深色格子
            }
            
            // 绘制整个格子背景（两个字符宽度）
            addch(' ');
            addch(' ');
            
            // 绘制棋子
            draw_piece(row, col, game->board[row][col]);
            
            attroff(COLOR_PAIR(1) | COLOR_PAIR(2));
        }
        
        // 右侧行号
        printw(" %d", 8 - row);
        printw("\n");
    }
    
    // 底部坐标标签 - 每个字母之间增加空格
    printw("   ");
    for (char c = 'a'; c <= 'h'; c++) {
        printw("%c  ", c);  // 两个空格而不是一个
    }
    printw("\n"); 
    // 显示当前玩家
    if (game->white_to_move) {
        printw("\nWhite to move");
    } else {
        printw("\nBlack to move");
    }
    
    // 显示AI状态
    if (game->ai_thinking) {
        printw(" - AI is thinking...");
    }
    
    // 显示将军状态
    if (game->white_in_check) {
        printw(" - WHITE KING IN CHECK!");
    } else if (game->black_in_check) {
        printw(" - BLACK KING IN CHECK!");
    }
    printw("\n");
    
    // 操作提示
    if (!game->game_over) {
        printw("\nInstructions:\n");
        printw("1. Use mouse to select piece and destination\n");
        printw("2. Press 'q' to quit to main menu\n");
        printw("3. Press 's' to switch sides\n");
        printw("4. Press 'd' to change AI difficulty\n");
    }
    
    // 显示游戏结束状态
    if (game->game_over) {
        printw("\nGAME OVER! ");
        if (game->winner == 'W') {
            printw("White wins!");
        } else if (game->winner == 'B') {
            printw("Black wins!");
        } else if (game->winner == 'D') {
            printw("Draw!");
        }
        printw("\nPress any key to return to main menu\n");
    }
    
    // 如果正在升变，显示提示
    if (game->pending_promotion) {
        printw("\nPromote pawn to: (Q)ueen, (R)ook, (B)ishop, k(N)ight\n");
    }
    
    refresh();
}

void draw_piece(int row, int col, char piece) {
    // 计算屏幕坐标
    int screen_row = row + 2; // 棋盘从第3行开始
    int screen_col = col * 2 + 3; // 棋盘从第3列开始，每个格子占2个字符宽度
    
    // 移动到正确位置
    move(screen_row, screen_col);
    
    // 绘制棋子 - 使用Unicode符号
    switch (piece) {
        case WHITE_PAWN: addstr("♙"); break;
        case WHITE_ROOK: addstr("♖"); break;
        case WHITE_KNIGHT: addstr("♘"); break;
        case WHITE_BISHOP: addstr("♗"); break;
        case WHITE_QUEEN: addstr("♕"); break;
        case WHITE_KING: addstr("♔"); break;
        case BLACK_PAWN: addstr("♟"); break;
        case BLACK_ROOK: addstr("♜"); break;
        case BLACK_KNIGHT: addstr("♞"); break;
        case BLACK_BISHOP: addstr("♝"); break;
        case BLACK_QUEEN: addstr("♛"); break;
        case BLACK_KING: addstr("♚"); break;
        default: addstr(" "); break;
    }
}

void draw_selection(int row, int col, bool selected) {
    // 计算屏幕坐标
    int screen_row = row + 2;
    int screen_col = col * 2 + 3;
    
    // 移动到正确位置
    move(screen_row, screen_col);
    
    if (selected) {
        // 高亮显示选中的格子
        attron(A_REVERSE);
        // 重新绘制棋子
        char piece = ' ';
        // 这里需要获取当前棋子的信息，但由于没有传入game参数，我们无法直接获取
        // 这个函数需要修改签名或通过其他方式获取棋子信息
        addch(' ');
        attroff(A_REVERSE);
    }
}

void draw_status_bar(ChessGame *game, bool is_ai_game) {
    // 移动到状态栏位置 (假设在棋盘下方)
    move(12, 0);
    clrtoeol();
    
    // 显示游戏模式
    if (is_ai_game) {
        if (game->player_is_white) {
            printw("Mode: Player(White) vs AI(Black)");
        } else {
            printw("Mode: Player(Black) vs AI(White)");
        }
    } else {
        printw("Mode: Online PVP");
    }
    
    // 显示当前玩家
    if (game->white_to_move) {
        printw(" - White to move");
    } else {
        printw(" - Black to move");
    }
    
    // 显示AI状态
    if (is_ai_game && game->ai_thinking) {
        printw(" - AI is thinking...");
    }
    
    // 显示将军状态
    if (game->white_in_check) {
        printw(" - WHITE KING IN CHECK!");
    } else if (game->black_in_check) {
        printw(" - BLACK KING IN CHECK!");
    }
    
    // 显示游戏结束状态
    if (game->game_over) {
        printw(" - GAME OVER! ");
        if (game->winner == 'W') {
            printw("White wins!");
        } else if (game->winner == 'B') {
            printw("Black wins!");
        } else if (game->winner == 'D') {
            printw("Draw!");
        }
    }
}

void draw_promotion_dialog(ChessGame *game) {
    // 在棋盘中央显示升变对话框
    int center_y = 5;
    int center_x = 15;
    
    // 绘制对话框背景
    attron(COLOR_PAIR(3)); // 使用不同的颜色对
    for (int y = center_y - 2; y <= center_y + 2; y++) {
        move(y, center_x - 10);
        for (int x = center_x - 10; x <= center_x + 10; x++) {
            addch(' ');
        }
    }
    attroff(COLOR_PAIR(3));
    
    // 绘制对话框边框
    attron(A_BOLD);
    move(center_y - 2, center_x - 10);
    addch(ACS_ULCORNER);
    for (int x = center_x - 9; x < center_x + 10; x++) {
        addch(ACS_HLINE);
    }
    addch(ACS_URCORNER);
    
    for (int y = center_y - 1; y <= center_y + 1; y++) {
        move(y, center_x - 10);
        addch(ACS_VLINE);
        move(y, center_x + 10);
        addch(ACS_VLINE);
    }
    
    move(center_y + 2, center_x - 10);
    addch(ACS_LLCORNER);
    for (int x = center_x - 9; x < center_x + 10; x++) {
        addch(ACS_HLINE);
    }
    addch(ACS_LRCORNER);
    attroff(A_BOLD);
    
    // 显示提示文字
    move(center_y, center_x - 8);
    printw("Promote pawn to:");
    
    // 显示选项
    move(center_y + 1, center_x - 8);
    printw("(Q)ueen (R)ook (B)ishop k(N)ight");
}

// 移除不需要的空白函数实现
// 只保留核心的UI功能

void show_game_screen(ChessGame *game, bool is_ai_game) {
    // 初始化颜色
    if (has_colors()) {
        start_color();
        init_pair(1, COLOR_WHITE, COLOR_BLUE);  // 浅色格子
        init_pair(2, COLOR_BLACK, COLOR_CYAN);  // 深色格子
        init_pair(3, COLOR_WHITE, COLOR_BLACK); // 对话框
    }
    
    // 游戏主循环
    bool quit = false;
    while (!quit && !game->game_over) {
        draw_chess_board(game);
        draw_status_bar(game, is_ai_game);
        
        // 处理输入
        int ch = getch();
        switch (ch) {
            case 'q':
                quit = true;
                break;
            case 's':
                if (is_ai_game) {
                    game->player_is_white = !game->player_is_white;
                }
                break;
            case 'd':
                if (is_ai_game) {
                    game->ai_difficulty = (game->ai_difficulty % 3) + 1;
                }
                break;
            case KEY_MOUSE:
                // 处理鼠标点击
                break;
        }
        
        // AI回合
        if (is_ai_game && 
            ((game->player_is_white && !game->white_to_move) || 
             (!game->player_is_white && game->white_to_move))) {
            ai_move(game);
        }
    }
    
    // 游戏结束处理
    if (game->game_over) {
        draw_chess_board(game);
        draw_status_bar(game, is_ai_game);
        printw("\nPress any key to continue...");
        getch();
    }
}

// 其他辅助函数
void handle_game_input(ChessGame *game, bool is_ai_game) {
    // 处理游戏输入
    int ch = getch();
    switch (ch) {
        case 'q':
            // 退出游戏
            break;
        case 's':
            if (is_ai_game) {
                game->player_is_white = !game->player_is_white;
            }
            break;
        case 'd':
            if (is_ai_game) {
                game->ai_difficulty = (game->ai_difficulty % 3) + 1;
            }
            break;
        case KEY_MOUSE:
            {
                MEVENT event;
                if (getmouse(&event) == OK) {
                    // 计算棋盘坐标
                    int row = event.y - 2;
                    int col = (event.x - 3) / 2;
                    
                    // 处理棋盘点击
                    if (row >= 0 && row < 8 && col >= 0 && col < 8) {
                        // 这里需要实现棋子选择和移动逻辑
                    }
                }
            }
            break;
    }
}
